#coding=utf-8
import pygame
from pygame.sprite import Sprite
class Ship(Sprite):
    def __init__(self,setting,screen,path):
        '''飞船的设置'''
        super(Ship,self).__init__()
        
        self.screen=screen
        self.setting=setting
        
        #加载飞船图形和外接矩形
        self.image=pygame.image.load(path + r'\\image\\player_plane.png')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()
        
        #把飞船放到屏幕的底部中央
        self.rect.centerx=self.screen_rect.centerx
        self.rect.bottom=self.screen_rect.bottom
        
        #飞船移动方向
        self.moving_right=False
        self.moving_left=False
        
        #飞船位置，浮点型储存
        self.center=float(self.rect.centerx)#水平位置,初始位置
        
    #设置位置的方法
    def update(self):
        if self.moving_right and self.rect.right < self.screen_rect.right :
            self.center += self.setting.ship_speed_factor
        if self.moving_left and self.rect.left > 0:
            self.center -= self.setting.ship_speed_factor
        
        #更新rect.centerx,更新位置
        self.rect.centerx = self.center
    def blitme(self):
        '''在指定位置绘制飞船'''
        self.screen.blit(self.image,self.rect)
    
    def center_ship(self):
        '''让飞船在屏幕上左右居中'''
        self.center = self.screen_rect.centerx